﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;
using System.Runtime.InteropServices;
using System.Xml;
namespace Presentation
{
    public partial class Form2 : Form
    {
        int DocGiaID = -1;
        int Rowselect = -1;
        public Form2()
        {
            InitializeComponent();
        }
        private void Load_Grid()
        {
            List<DocGiaDTO> listdocgia = DocGiaService.GetData();
            dgvdocgia.DataSource = listdocgia;
        }
        private void Load_Cbx()
        {
            List<LoaiDocGiaDTO> listloaidocgia = LoaiDocGiaService.GetData();
            cbxloaidocgia.DisplayMember = LoaiDocGiaColumns.TenLoaiDocGia;
            cbxloaidocgia.ValueMember = LoaiDocGiaColumns.MaLoaiDocGia;
            cbxloaidocgia.DataSource = listloaidocgia;
            cbxloaidocgia.SelectedIndex = 0;
        }
        private void Form2_Load(object sender, EventArgs e)
        {            
            Load_Grid();
            Load_Cbx();  
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            DocGiaDTO docgiaDTO = new DocGiaDTO();
            docgiaDTO.TenDocGia = txttendocgia.Text;
            docgiaDTO.MaLoaiDocGia = Convert.ToInt32(cbxloaidocgia.SelectedValue.ToString());
            docgiaDTO.DiaChi = txtdiachi.Text;
            docgiaDTO.NgayLap = Convert.ToDateTime(dtngaylap.Value.ToString());
            docgiaDTO.HanSuDung = Convert.ToDateTime(dthansudung.Value.ToString());
            docgiaDTO.NgaySinh = Convert.ToDateTime(dtngaysinh.Value.ToString());
            docgiaDTO.Email = txtemail.Text;
            docgiaDTO.MaNguoiLap = Convert.ToInt32(txtmanguoilap.Text);
            DocGiaService.Them(docgiaDTO);
            Load_Grid();
            
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            Rowselect = dgvdocgia.CurrentRow.Index;
            DocGiaID = Convert.ToInt32(dgvdocgia.Rows[Rowselect].Cells[0].Value);
            DocGiaService.Xoa(DocGiaID);
            Load_Grid();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtngaylap_ValueChanged(object sender, EventArgs e)
        {
            

        }

        private void dgvdocgia_SelectionChanged(object sender, EventArgs e)
        {
            Rowselect = dgvdocgia.CurrentRow.Index;
            DocGiaID = Convert.ToInt32(dgvdocgia.Rows[Rowselect].Cells[0].Value);
            DocGiaDTO docgia = new DocGiaDTO();
            docgia = DocGiaService.GetDocGia(DocGiaID);
            txttendocgia.Text = docgia.TenDocGia;
            txtdiachi.Text = docgia.DiaChi;
            txtemail.Text = docgia.Email;
            txtmanguoilap.Text = Convert.ToString(docgia.MaNguoiLap);
            dtngaylap.Value = docgia.NgayLap;
            dtngaysinh.Value = docgia.NgaySinh;
            dthansudung.Value = docgia.HanSuDung;
            cbxloaidocgia.Text = Convert.ToString(docgia.MaLoaiDocGia);

        }
    }
}
